/*

   Copyright(c) 2008 Acatl Pacheco
   e-mail:   acatl.pacheco@gmail.com

   This file is part of Teztoc.

   Oztoc is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   Oztoc is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with Oztoc. If not, see <http://www.gnu.org/licenses/>.

 */

package net.teztoc
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.SharedObject;
	import flash.utils.getQualifiedClassName;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.controls.Label;
	import mx.controls.LinkButton;
	import mx.controls.Tree;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	
	import net.teztoc.vo.TestHarnessElementVO;

	public class TestHarnessBase extends Canvas
	{
		public var reload:LinkButton;
		public var currentComponentName:Label;
		public var container:Canvas;

		public var componentsExplorer:Tree;
		
		[Bindable]
		public var currentNotes:String;

		[Bindable]
		public var componentProvider:XML;

		private var currentComponent:UIComponent;

		private var currentTestHarness:TestHarnessElementVO;

		/**
		 * Constructor
		 * */
		public function TestHarnessBase()
		{
			super();
			data=new ArrayCollection();
		}

		public var _preferences:SharedObject;

		private function get preferences():SharedObject
		{
			if (_preferences == null)
			{
				_preferences=SharedObject.getLocal("TestHarnessUI");
			}
			return _preferences;
		}

		public function get lastIndex():Number
		{
			var index:Number=preferences.data.lastIndex;
			if (isNaN(index))
			{
				preferences.data.lastIndex=-1;
				preferences.flush();
			}
			index=preferences.data.lastIndex;
			return index;
		}


		override protected function childrenCreated():void
		{
			super.childrenCreated();
		}

		protected function addTestHarnessElement(testHarness:Class, name:String=null):TestHarnessElementVO
		{
			var className:String=name != null ? name : getClassName(testHarness);

			var th:TestHarnessElementVO=new TestHarnessElementVO(className, testHarness);
			data.addItem(th);

			return th;
		}
		
		private function getClassName(testHarness:Class):String
		{
			var name:String = getQualifiedClassName(testHarness);
			return name.substring(name.indexOf("::"));
		}

		public function addTestHarness(group:String, testHarness:Class, name:String=null):void
		{
			if (group == "")
				group="Uncategorized";

			var thElement:TestHarnessElementVO=addTestHarnessElement(testHarness, name);

			if (componentProvider == null)
				componentProvider=<explorer label="Components"/>;

			var componentGroup:XML;

			var groupList:XMLList=componentProvider.group.(@label == group);

			if (groupList.length() == 0)
			{
				componentGroup=<group/>
				componentGroup.@label=group;
				componentProvider.appendChild(componentGroup);
			}
			else
			{
				componentGroup=groupList[0];
			}

			var testComponent:XML=<testComponent/>

			testComponent.@label=thElement.name;
			testComponent.@uid=thElement.uid;


			componentGroup.appendChild(testComponent);

			componentsExplorer.dataProvider=componentProvider.group;

			componentsExplorer.invalidateList();
			componentsExplorer.validateNow();
			
			for (var i:int=0; i < componentsExplorer.dataProvider.length; i++)
			{
				componentsExplorer.expandChildrenOf(componentsExplorer.dataProvider[i], true)
			}
		}

		protected function itemClickHandler(event:Event):void
		{
			var node:XML=componentsExplorer.selectedItem as XML;
			if (node.name().toString() == "testComponent")
			{
				for each (var element:TestHarnessElementVO in data)
				{
					if (element.uid == node.@uid)
					{
						currentTestHarness=element;
						updateTestHarness(currentTestHarness);
						return;
					}
				}
			}
		}

		protected function updateTestHarness(element:TestHarnessElementVO):void
		{
			if (currentComponent)
			{
				container.removeChild(currentComponent);
			}

			var generator:Class=element.generator;

			currentComponentName.text=element.longName;

			currentComponent=new generator();
			currentComponent.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
			container.addChild(currentComponent);


			reload.enabled=true;
		}
		
		private function creationCompleteHandler(event:FlexEvent):void
		{
			currentComponent.removeEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
			currentNotes = currentComponent.hasOwnProperty("testHarnessNotes") ? currentComponent["testHarnessNotes"] : "";
		}


		/**
		 * Updates the testharness relaoding the current class
		 **/
		protected function reloadHandler(event:MouseEvent):void
		{
			updateTestHarness(currentTestHarness);
		}

	}
}